package com.meta.nd.controller;

import java.util.List;

import com.meta.common.CommonInfo;
import com.meta.nd.vo.*;
import com.meta.util.ExceptionUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.meta.nd.service.NvBusinessService;
import com.meta.nd.service.NvService;



@Slf4j
@RestController
public class NvController {
	
	private static Logger logger = Logger.getLogger(NvController.class);
	@Autowired
	NvService nvService;
	@Autowired
	NvBusinessService nvBusinessService;

	@RequestMapping(value = "/nv/getNv", method=RequestMethod.POST)
	public ReturnMessage<Nv> getNv(  @RequestBody RequestMessage<FetchNvParam> requestMessage) {
		FetchNvParam param = requestMessage.getValue();
		CommonInfo commonInfo = requestMessage.getCommonInfo();
		try {
			Nv nv;
			if ( param.getNvId() > 0 )
				nv = nvService.getNv(commonInfo,param.getNdCode(), param.getNvId());
			else if (!StringUtils.isEmpty(param.getCode()))
				nv = nvService.getNvByCode(commonInfo,param.getCode(),param.getCodeFdName(), param.getNdCode());
			else
				throw new RuntimeException("或是实例的入参错误哦!");
			return new ReturnMessage<Nv>(nv,true);
		}
		catch ( Exception e ) {
			logger.error(e.getMessage(),e);
			return ReturnMessage.buildFailMessage(e.getMessage(),ExceptionUtils.getStackTrace(e));
		}
	}



	@RequestMapping(value = "/nv/copyNv", method=RequestMethod.POST)
	public ReturnMessage<Nv> copyNv(  @RequestBody RequestMessage<FetchNvParam> requestMessage) {
		FetchNvParam param = requestMessage.getValue();
		CommonInfo commonInfo = requestMessage.getCommonInfo();
		try {
			Nv nv;
			if ( param.getNvId() > 0 )
				nv = nvService.copyNvById(commonInfo,param.getNdCode(), param.getNvId());
			else if (!StringUtils.isEmpty(param.getCode())) {
				nv = nvService.getNvByCode(commonInfo,param.getCode(), param.getCodeFdName(), param.getNdCode());
				nv = nvService.copyNv(commonInfo,nv);
			}
			else
				throw new RuntimeException("或是实例的入参错误哦!");

			return new ReturnMessage<Nv>(nv,true);
		}
		catch ( Exception e ) {
			logger.error(e.getMessage(),e);
			return ReturnMessage.buildFailMessage(e.getMessage(),ExceptionUtils.getStackTrace(e));
		}
	}



	
	@RequestMapping(value = "/nv/saveNv",method=RequestMethod.POST) 	
	public ReturnMessage<Nv> saveNv( @RequestBody RequestMessage<Nv> requestMessage) {
		CommonInfo commonInfo = requestMessage.getCommonInfo();
		Nv nv = requestMessage.getValue();
		try {
			this.nvService.saveNv(commonInfo,nv);
			return new ReturnMessage<Nv>(nv,true);
			
		} catch (Exception e ) {
			logger.error(e,e);
			return ExceptionUtils.buildReturnMessage(e);
		}	
	}


	@RequestMapping(value = "/nv/saveNvs",method=RequestMethod.POST)
	public ReturnMessage<List<Nv>> saveNvs( @RequestBody RequestMessage<List<Nv>> requestMessage) {
		CommonInfo commonInfo = requestMessage.getCommonInfo();
		List<Nv> nvs = requestMessage.getValue();
		try {
			this.nvService.saveNvs(commonInfo,nvs);
			return new ReturnMessage<List<Nv>>(nvs,true);

		} catch (Exception e ) {
			logger.error(e,e);
			return ExceptionUtils.buildReturnMessage(e);
		}
	}

	@RequestMapping(value = "/nv/commitNv",method=RequestMethod.POST)
	public ReturnMessage<Nv> commitNv( @RequestBody RequestMessage<Nv> requestMessage) {
		CommonInfo commonInfo = requestMessage.getCommonInfo();
		Nv nv = requestMessage.getValue();
		try {
			this.nvBusinessService.commitNv(commonInfo,nv);
			return new ReturnMessage<Nv>(nv,true);
		} catch (Exception e ) {
			logger.error(e,e);
			return ExceptionUtils.buildReturnMessage(e);
		}
	}

	@RequestMapping(value = "/nv/commitNvs",method=RequestMethod.POST)
	public ReturnMessage<List<Nv>> commitNvs( @RequestBody RequestMessage<List<Nv>> requestMessage) {
		CommonInfo commonInfo = requestMessage.getCommonInfo();
		List<Nv> nvs = requestMessage.getValue();
		try {
			this.nvBusinessService.commitNvs(commonInfo,nvs);
			return new ReturnMessage<List<Nv>>(nvs,true);
		} catch (Exception e ) {
			logger.error(e.getMessage(),e);
			return ExceptionUtils.buildReturnMessage(e);
		}
	}
	
	@RequestMapping(value="/nv/initNv", method = RequestMethod.POST)
	public ReturnMessage<Nv> initNv (@RequestBody RequestMessage<String> requestMessage ) {
		CommonInfo commonInfo = requestMessage.getCommonInfo();
		String ndCode = requestMessage.getValue();
		ReturnMessage<Nv> returnMessage = new ReturnMessage<Nv>();
		try {
			Nv nv = this.nvService.initNv(commonInfo,ndCode);
			returnMessage.setValue(nv);
			return returnMessage;
		} catch (Exception e ) {
			logger.error(e.getMessage(),e);
			returnMessage.setMessage(e.getMessage());
			returnMessage.setDetailMessage(ExceptionUtils.getStackTrace(e));
			return returnMessage;
		}
	}


	@RequestMapping(value = "/nv/deleteNv", method=RequestMethod.POST)
	public ReturnMessage<Integer> deleteNv(  @RequestBody  RequestMessage<FetchNvParam> requestMessage) {
		FetchNvParam fetchNvParam = requestMessage.getValue();
		CommonInfo commonInfo = requestMessage.getCommonInfo();
		try {		
			Long id = fetchNvParam.getNvId();
			String ndNode = fetchNvParam.getNdCode();
			 int count =  nvService.deleteNv(commonInfo,ndNode, id);
			 return new ReturnMessage<Integer> (true, null, count);
		} catch (Exception e ) {	
			logger.error(e.getMessage(),e);
			return new ReturnMessage<Integer> (e.getMessage());
		}
	}

	@RequestMapping(value = "/nv/getParentNvIdCodeAndNdCode", method=RequestMethod.POST)
	public ReturnMessage<Long> getParentNvIdCodeAndNdCode (  @RequestBody  RequestMessage<FetchNvParam> requestMessage) {
		FetchNvParam fetchNvParam = requestMessage.getValue();
		CommonInfo commonInfo = requestMessage.getCommonInfo();
		try {
			String codeFdName = fetchNvParam.getCodeFdName();
			String ndNode = fetchNvParam.getNdCode();
			String parentNvCode = fetchNvParam.getCode();
			long id =  nvService.getParentNvIdCodeAndNdCode(commonInfo,parentNvCode,codeFdName,ndNode);
			return new ReturnMessage<Long> (true, null, id);
		} catch (Exception e ) {
			logger.error(e.getMessage(),e);
			return new ReturnMessage<Long> (e.getMessage());
		}
	}


	@RequestMapping(value = "/nv/resetId", method=RequestMethod.POST)
	public ReturnMessage<Long> resetId (  @RequestBody  RequestMessage<String> requestMessage) {
		String ndCode = requestMessage.getValue();
		CommonInfo commonInfo = requestMessage.getCommonInfo();
		try {
			nvService.resetId(commonInfo,ndCode);
			return new ReturnMessage<Long> (true, null, 0L);
		} catch (Exception e ) {
			logger.error(e.getMessage(),e);
			return new ReturnMessage<Long> (e.getMessage());
		}
	}
}
